package com.jd.risk.bankorder.controller.privilege;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jd.risk.bankorder.common.CommonResult;
import com.jd.risk.bankorder.controller.BaseController;
import com.jd.risk.bankorder.entity.privilege.User;
import com.jd.risk.bankorder.service.privilege.RoleService;
import com.jd.risk.bankorder.service.privilege.UserService;


@Controller
@RequestMapping("/privilege/user")
public class UserController extends BaseController{

	
	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/toUserPage")
	public String toUserListPage()
	{
		return "/privilege/userListPage";
	}
	
	@RequestMapping("/toUserAddPage")
	public String toUserAddPage()
	{
		return "/privilege/userAddPage";
	}
	
	@RequestMapping("/toModifyPassword")
	public String toModifyPassword(ModelMap map ,Long id)
	{
		User user = userService.getUser(id);
		map.put("user", toJson(user));
		return "/privilege/modifyPassword";
	}
	
	@ResponseBody
	@RequestMapping("/modifyPassword")
	public String modifyPassword(User user)
	{
		
		CommonResult<String> result = new CommonResult<String>();
		int ret = userService.updateUser(user);
		if(ret >0)
		{
			result.setSuccess(true);
		}else{
			result.setSuccess(false);
			result.setMessage("修改密码失败！");
		}
		return toJson(result) ;
	}
	
	@ResponseBody
	@RequestMapping("/queryUserList")
	public String queryUserList()
	{
		CommonResult<User> res = userService.getAllUser();
		return toJson(res);
	}
	
	
	@ResponseBody
	@RequestMapping("/userAdd")
	public String userAdd(User user)
	{
		CommonResult<String> res = userService.userAdd(user);
		
		return toJson(res);
	}
	
	@RequestMapping("/toUserAuthorization")
	public String toUserAuthorization(ModelMap map , Long id)
	{
		map.put("user", toJson(userService.getUser(id)));
		map.put("roles", toJson(roleService.getAllRoleList()));
		return "/privilege/userAuthorization";
	}
	
	@ResponseBody
	@RequestMapping("userAuthorization")
	public String userAuthorization(User user)
	{
		CommonResult<String> res  = new CommonResult<String>();
		int ret  = userService.userAuthorization(user);
		if(ret >0)
		{
			res.setSuccess(true);
		}else{
			res.setSuccess(false);
			res.setMessage("授权失败");
		}
		return toJson(res);
	}
	
}
